<?php 
/*
 module:		因子
 create_time:	2024-04-18 14:19:50
 author:		
 contact:		
*/

namespace app\admin\controller\Mbti;

use app\admin\service\Mbti\FactorService;
use app\admin\model\Mbti\Factor as FactorModel;
use app\admin\controller\Admin;
use think\facade\Db;

class Factor extends Admin {


	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$where = [];
			$where['mbti_id'] = $this->request->param('mbti_id', '', 'serach_in');

			$order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc

			$field = 'mf_id,factor_name,factor_iden,create_time,edit_time';
			$orderby = ($sort && $order) ? $sort.' '.$order : 'mf_id desc';

			$res = FactorService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
			return json($res);
		}
	}

	/*添加*/
	function add(){
		if (!$this->request->isPost()){
			return view('add');
		}else{
			$postField = 'factor_name,factor_iden,create_time,mbti_id';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = FactorService::add($data);
			return json(['status'=>'00','msg'=>'添加成功']);
		}
	}

	/*修改*/
	function edit(){
		if (!$this->request->isPost()){
			$mf_id = $this->request->get('mf_id','','serach_in');
			if(!$mf_id) $this->error('参数错误');
			$this->view->assign('info',checkData(FactorModel::find($mf_id)));
			return view('edit');
		}else{
			$postField = 'mf_id,factor_name,factor_iden,edit_time,mbti_id';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = FactorService::edit($data);
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

	/*删除*/
	function del(){
		$idx =  $this->request->post('mf_id', '', 'serach_in');
		if(!$idx) $this->error('参数错误');
		try{
			FactorModel::destroy(['mf_id'=>explode(',',$idx)],true);
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}



}

